#include "linktab.h"



int walk(LT_NODE *L) //打印
{
    LT_NODE *p = L;
    int length = 0;
    p = p->next;
    while (p)
    {
        printf("%d ", p->data);
        p = p->next;
        length++;
    }
    if (length == 0)
        printf("NULL");
    printf("\n");
    return length;
}

void ListAppend(LT_NODE *L, int elem) //在链表L最后插入元素
{
    LT_NODE *p = L;
    while (p->next)
    {
        p = p->next;
    }
    LT_NODE *new_node;
    new_node = (LT_NODE *)malloc(sizeof(LT_NODE)); //插入新节点
    new_node->data = elem;
    new_node->next = NULL;
    p->next = new_node;
}

int ListDelete(LT_NODE *L, int i) //删除链表L第i个位置的元素并释放内存
{
    LT_NODE *p = L;
    int length = 0;
    while (p)
    {
        p = p->next;
        length++;
    }
    if (i >= length)
        return 0; //防止越界
    p = L;
    int j = 0;
    while (p && j < i - 1)
    {
        p = p->next;
        ++j;
    }
    free(p->next);
    p->next = p->next->next;
    return 1;
}
int ListDestroy(LT_NODE *L)//销毁链表
{
    if(L==NULL)
    return 0;
    LT_NODE *p;
    while(L)
    {
       p=L->next;
       free(L);
       L=p;
    }
    return 1;
}
int ClearList(LT_NODE *L)  //清空链表
{  
    LT_NODE *p,*q;  
    if(L==NULL)  
        return 0;  
    p=L->next;  
    while(p)  
    {  
        q=p->next;  
        free(p);  
        p=q;  
    }  
    L->next=NULL;  
    return 1;  
}  